package com.playball.lottery.customview;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;

import com.playball.lottery.R;
import com.playball.lottery.util.AbstractActivity;
import com.playball.lottery.util.AppUtils;
import com.playball.lottery.util.Constants;

/**
 * 修改追号方案
 * 
 * @ClassName: IntelligenceChaseUpdatePlan
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 2w
 * @date Jul 24, 2014 3:50:04 PM
 * 
 */

public class IntelligenceChaseUpdatePlan extends AbstractActivity implements
		OnClickListener  {
	/** 取消 */
	private Button btnCancle;
	/** 生成方案 */
	private Button btnCreatePlan;

	// 连续追号
	/** 减期次 */
	private Button btnSubIssue;
	/** 加期次 */
	private Button btnAddIssue;
	/** 输入的期次 */
	private EditText edtInputIssue;

	// 起始倍数
	/** 减倍数 */
	private Button btnSubMul;
	/** 加倍数 */
	private Button btnAddMul;
	/** 输入的倍数 */
	private EditText edtInputMul;

	// 预期盈利
	/** 全程最低盈利率 按钮 */
	private RadioButton rBtnIncomeRate;
	/** 全程最低盈利率 输入 */
	private EditText edtIncomeRate;
	/** 全程最低盈利 按钮 */
	private RadioButton rBtnIncome;
	/** 全程最低盈利 输入 */
	private EditText edtIncome;

	/** 中奖后停止追号 */
	private CheckBox cBoxWinStopChase;
	
	/**期数*/
	private int intInputIssue ;
	/**倍数*/
	private int intInputMul ;
	/**0 继续追号  1停止 追号*/
	private int intWinStopChase = 1;
	/**选择类型   1. 全程盈利率 2 累计盈利不低于 */
	private int model = 1;
	
	/**彩种 id*/
	private int lorID ;
	/**当前 期次*/
	private String issueName;
	/**取出后面的2位 转换 整数*/
	private int issueInt ;
	
	/**最大 倍数*/
	private int issueMaxMul = 999 ;
	
	/**最大 盈利*/
	private int rateMaxMul = 99999 ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buy_intelligence_chase_update_plan);
		initView();
		initData();
	}

	
	private void initView() {
		// TODO Auto-generated method stub
		btnCancle = (Button) findViewById(R.id.btn_intell_cancle);
		btnCancle.setOnClickListener(this);
		btnCreatePlan = (Button) findViewById(R.id.btn_intell_create_plan);
		btnCreatePlan.setOnClickListener(this);

		btnSubIssue = (Button) findViewById(R.id.btn_intell_sub_issue);
		btnSubIssue.setOnClickListener(this);
		btnAddIssue = (Button) findViewById(R.id.btn_intell_add_issue);
		btnAddIssue.setOnClickListener(this);
		edtInputIssue = (EditText) findViewById(R.id.edt_intell_input_issue);

		btnSubMul = (Button) findViewById(R.id.btn_intell_sub_mul);
		btnSubMul.setOnClickListener(this);
		btnAddMul = (Button) findViewById(R.id.btn_intell_add_mul);
		btnAddMul.setOnClickListener(this);
		edtInputMul = (EditText) findViewById(R.id.edt_intell_input_mul);

		rBtnIncomeRate = (RadioButton) findViewById(R.id.btn_expect_incomeRate);
		rBtnIncomeRate.setChecked(true);
		edtIncomeRate = (EditText) findViewById(R.id.edt_intell_input_expect_incomeRate);
		rBtnIncome = (RadioButton) findViewById(R.id.btn_expect_income);
		edtIncome = (EditText) findViewById(R.id.edt_intell_input_expect_income);

		cBoxWinStopChase = (CheckBox) findViewById(R.id.check_win_stop_chase);
		
		lorID = getIntent().getIntExtra("lorID", 0);
		issueName = getIntent().getStringExtra("issueName");
		issueName = issueName.substring(issueName.length()-2, issueName.length());
		issueInt = Integer.parseInt(issueName);
		// 各种监听
		btnAndEdtListner();
	}

	
	private void initData() {
		edtInputIssue.setText(getIntent().getIntExtra("castIsuse", 10)+"");
		edtInputMul.setText(getIntent().getIntExtra("inputMutiple", 1)+"");
		edtIncomeRate.setText(getIntent().getIntExtra("expectIncomeRate", 30)+"");
		edtIncome.setText(getIntent().getIntExtra("expectIncome", 100)+"");
		
		model =  getIntent().getIntExtra("model", 1);
		if (model == 1 ) {
			edtIncomeRate.setEnabled(true);
			edtIncome.setEnabled(false);
			rBtnIncomeRate.setChecked(true);
		}else {
			edtIncomeRate.setEnabled(false);
			edtIncome.setEnabled(true);
			rBtnIncome.setChecked(true);
		}
		
		intWinStopChase = getIntent().getIntExtra("intWinStopChase", 1);
		if (intWinStopChase == 1) {
			cBoxWinStopChase.setChecked(true);
		}else {
			cBoxWinStopChase.setChecked(false);
		}
	}

	
	//按钮 编辑 监听
	private void btnAndEdtListner() {
		// TODO Auto-generated method stub
		rBtnIncomeRate.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

				if (isChecked) {
					model = 1 ;
					edtIncomeRate.setEnabled(true);
					edtIncome.setEnabled(false);
					edtIncomeRate.setFocusableInTouchMode(true);
					edtIncome.setFocusableInTouchMode(false);
					rBtnIncome.setChecked(false);
				}  
				
			}
		});

		rBtnIncome.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

				if (isChecked) {
					model = 2 ;
					edtIncomeRate.setEnabled(false);
					edtIncome.setEnabled(true);
					edtIncome.setFocusableInTouchMode(true);;
					edtIncomeRate.setFocusableInTouchMode(false);
					rBtnIncomeRate.setChecked(false);
				} 
				
			}
		});
		
		cBoxWinStopChase.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					intWinStopChase = 1 ;
				}else {
					intWinStopChase =  0 ;
				}
			}
		});
		
		//点击全选
/*		edtInputIssue.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (v.getId() == R.id.edt_intell_input_issue) {
					edtInputIssue.selectAll();
					Log.d("van", "	edtInputIssue.selectAll();");
					InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(v, 1);
					edtInputIssue.invalidate();
					return true;
				}
				return false;
			}
		}); */
  
		//期次监听
		edtInputIssue.addTextChangedListener(new TextWatcher() {
			private boolean isChanged = false;
			int inputIssue = 1;//输入的期次
			@Override
			public void afterTextChanged(Editable s) {
				if(isChanged){
		            return ;
		        }
				if (!AppUtils.isBlank(edtInputIssue.getText().toString().trim())) {
					if (s.toString().equals("")) {
						edtInputIssue.setText(Integer.toString(1));
					}else {
						inputIssue = Integer.parseInt(s.toString());
					}
					isChanged = true;
					//快3
					if (lorID == Constants.LOT_ID_KUAI3) {
						if (Integer.parseInt(s.toString())<1) {
							// showToast(R.string.input_least_issue);
							 edtInputIssue.setText(Integer.toString(1));
						}else if (Integer.parseInt(s.toString()) > (Constants.ISSUE_K_3_MAX-issueInt)) {
							 //showToast("超过今天最大期次!");
							 edtInputIssue.setText(Integer.toString(Constants.ISSUE_K_3_MAX-issueInt+1));
						} else {
							edtInputIssue.setText(Integer.toString(inputIssue));
						}
					}
					//11选5
					else if (lorID == Constants.LOT_ID_11X5) {
						if (Integer.parseInt(s.toString())<1) {
							 //showToast(R.string.input_least_issue);
							 edtInputIssue.setText(Integer.toString(1));
						}else if (Integer.parseInt(s.toString()) > (Constants.ISSUE_11_5_MAX-issueInt)) {
							// showToast("超过今天最大期次!");
							 edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_5_MAX-issueInt+1));
						}else {
							edtInputIssue.setText(Integer.toString(inputIssue));
						}
					}
					// 11运 夺金
					else if (lorID == Constants.LOT_ID_11YDJ) {
						if (Integer.parseInt(s.toString())<1) {
							// showToast(R.string.input_least_issue);
							 edtInputIssue.setText(Integer.toString(1));
						}else if (Integer.parseInt(s.toString()) > (Constants.ISSUE_11_DJ_MAX-issueInt)) {
							// showToast("超过今天最大期次!");
							 edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_DJ_MAX-issueInt + 1));
						}else {
							edtInputIssue.setText(Integer.toString(inputIssue));
						}
					}
					// 重庆时时彩
					else if (lorID == Constants.LOT_ID_CQSSC) {
						if (Integer.parseInt(s.toString())<1) {
							 //showToast(R.string.input_least_issue);
							 edtInputIssue.setText(Integer.toString(1));
						}else if (Integer.parseInt(s.toString()) > (Constants.ISSUE_CQSS_MAX-issueInt)) {
							// showToast("超过今天最大期次!");
							 edtInputIssue.setText(Integer.toString(Constants.ISSUE_CQSS_MAX-issueInt +1 ));
						}else {
							edtInputIssue.setText(Integer.toString(inputIssue));
						}
					}
					
					 edtInputIssue.setSelection(edtInputIssue.length());
					 isChanged = false;
					 edtInputIssue.invalidate();
				}else {
				//	edtInputIssue.setText("");
					
				}
			}
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
			
		});
		
		
		
	/*	edtInputMul.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (v.getId() == R.id.edt_intell_input_mul) {
					
					edtInputMul.selectAll();
					Log.d("van", "	edtInputMul.selectAll();");
					InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(v, 1);
					edtInputMul.invalidate();
					return true;
				}
				return false;
			}
		});*/
		//监听倍数
		edtInputMul.addTextChangedListener(new TextWatcher() {
			private boolean isChanged = false;
			int inputMul = 1;//输入的倍数
			@Override
			public void afterTextChanged(Editable s) {
				if(isChanged){
		            return ;
		        }
				if (!AppUtils.isBlank(edtInputMul.getText().toString().trim())) {
					if (s.toString().equals("")) {
						edtInputMul.setText(Integer.toString(1));
					}else {
						inputMul = Integer.parseInt(s.toString());
					}
					isChanged = true;
					if (Integer.parseInt(s.toString())<1) {
						 //showToast(R.string.input_least_mul);
						 edtInputMul.setText(Integer.toString(1));
					}else if(Integer.parseInt(s.toString())>issueMaxMul){
						//showToast("超过最大倍数");
						 edtInputMul.setText(""+issueMaxMul);
					}
					else {
						edtInputMul.setText(Integer.toString(inputMul));
					} 
					edtInputMul.setSelection(edtInputMul.length());
					 isChanged = false;
					 edtInputMul.invalidate();
				}else {
					//edtInputMul.setText(Integer.toString(1));
				}
			
			}
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
		});
		
		/*edtIncomeRate.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				 if (v.getId() == R.id.edt_intell_input_expect_incomeRate) {
						edtIncomeRate.selectAll();
						Log.d("van", "	edtIncomeRate.selectAll();");
						InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.showSoftInput(v, 0);
						return true;
					}
				return false;
			}
		});*/
		//全程最低盈利率 监听
		edtIncomeRate.addTextChangedListener(new TextWatcher() {
			private boolean isChanged = false;
			int inputIncomeRate = 1;//输入的倍数
			@Override
			public void afterTextChanged(Editable s) {

				if(isChanged){
		            return ;
		        }
				if (!AppUtils.isBlank(edtIncomeRate.getText().toString().trim())) {
					if (s.toString().equals("")) {
						edtIncomeRate.setText(Integer.toString(1));
					}else {
						inputIncomeRate = Integer.parseInt(s.toString());
					}
					isChanged = true;
					if (Integer.parseInt(s.toString())<1) {
						//showToast(R.string.input_least_rate);
						edtIncomeRate.setText(Integer.toString(1));
					}else if(Integer.parseInt(s.toString())>rateMaxMul){
						//showToast("超过全程最低盈利率范围");
						edtIncomeRate.setText(Integer.toString(rateMaxMul));
					}
					else {
						edtIncomeRate.setText(Integer.toString(inputIncomeRate));
					} 
					 edtIncomeRate.setSelection(edtIncomeRate.length());
					 isChanged = false;
					 edtIncomeRate.invalidate();
				}else {
				//	edtIncomeRate.setText(Integer.toString(1));
				}
			
			
			}
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
		});
		
		/*edtIncome.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (v.getId() == R.id.edt_intell_input_expect_income) {
					edtIncome.selectAll();
					Log.d("van", "	edtIncome.selectAll();");
					InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(v, 0);
					return true;
				}
				return false;
			}
		});*/
		//全程最低盈利  监听
		edtIncome.addTextChangedListener(new TextWatcher() {
			private boolean isChanged = false;
			int inputIncome = 1;//输入的
			@Override
			public void afterTextChanged(Editable s) {
				if(isChanged){
		            return ;
		        }
				if (!AppUtils.isBlank(edtIncome.getText().toString().trim())) {
					if (s.toString().equals("")) {
						edtIncome.setText(Integer.toString(1));
					}else {
						inputIncome = Integer.parseInt(s.toString());
					}
					isChanged = true;
					if (Integer.parseInt(s.toString())<1) {
						//showToast(R.string.input_least_income);
						edtIncome.setText(Integer.toString(1));
					}else if(Integer.parseInt(s.toString())>rateMaxMul){
						//showToast("超过全程最低盈利范围");
						edtIncome.setText(Integer.toString(rateMaxMul));
					}else {
						edtIncome.setText(Integer.toString(inputIncome));
					} 
					edtIncome.setSelection(edtIncome.length());
					 isChanged = false;
					 edtIncome.invalidate();
				}else {
					//edtIncome.setText(Integer.toString(1));
				}
			}
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
		});
		
		
	}
	
	
	//输入框的触摸
	/*@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (v.getId() == R.id.edt_intell_input_issue) {
			edtInputIssue.selectAll();
			Log.d("van", "	edtInputIssue.selectAll();");
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(v, 0);
			return true;
		}else if (v.getId() == R.id.edt_intell_input_mul) {
			edtInputMul.selectAll();
			Log.d("van", "	edtInputMul.selectAll();");
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(v, 0);
			return true;
		}else if (v.getId() == R.id.edt_intell_input_expect_incomeRate) {
			edtIncomeRate.selectAll();
			Log.d("van", "	edtIncomeRate.selectAll();");
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(v, 0);
			return true;
		}else if (v.getId() == R.id.edt_intell_input_expect_income) {
			edtIncome.selectAll();
			Log.d("van", "	edtIncome.selectAll();");
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(v, 0);
			return true;
		}
		 
			return false;
	}
	*/

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_intell_cancle:
			IntelligenceChaseUpdatePlan.this.finish();

			break;
			
			//生成方案
		case R.id.btn_intell_create_plan:
			
			
		
			//期次
			if (AppUtils.isBlank(edtInputIssue.getText().toString().trim())) {
				showToast("请设置您的期次");
				return  ;
			}
			//倍数
			if (AppUtils.isBlank(edtInputMul.getText().toString().trim())) {
				showToast("请设置您的倍数");
				return  ;
			}
			
	       if (model == 1 ) {
	    	 //全程最低盈利
	    	   if (AppUtils.isBlank(edtIncome.getText().toString().trim())) {
					edtIncome.setText(Integer.toString(100));
				}
	    	 //全程最低盈利率
				if (AppUtils.isBlank(edtIncomeRate.getText().toString().trim())) {
					showToast("请设置您的全程最低盈利率");
					return  ;
				}
			}else {
				//全程最低盈利
				if (AppUtils.isBlank(edtIncome.getText().toString().trim())) {
					showToast("请设置您的全程最低盈利");
					return  ;
				}
				 //全程最低盈利率
				if (AppUtils.isBlank(edtIncomeRate.getText().toString().trim())) {
					edtIncomeRate.setText(Integer.toString(30));
				}
				
			}
			
			
			Intent data = new Intent() ;
			data.putExtra("inputIssue", edtInputIssue.getText().toString().trim());
			data.putExtra("inputMul", edtInputMul.getText().toString().trim());
			data.putExtra("inputIncomeRate", edtIncomeRate.getText().toString().trim());
			data.putExtra("inputIncome", edtIncome.getText().toString().trim());
			data.putExtra("intWinStopChase", intWinStopChase);
			data.putExtra("model", model);
			Log.d("van", edtInputIssue.getText().toString().trim() +"--" +edtInputMul.getText().toString().trim()
					+ "---" + edtIncomeRate.getText().toString().trim() +"---" +edtIncome.getText().toString().trim()
					+"-----"+model +"--  intWinStopChase--"+intWinStopChase);
			setResult(20000, data);
			IntelligenceChaseUpdatePlan.this.finish();
			break;

		case R.id.btn_intell_sub_issue:
			intInputIssue = Integer.parseInt(edtInputIssue.getText().toString().trim())	 ;
			if (intInputIssue < 1) {
				edtInputIssue.setText("1");
			}else {
				
				intInputIssue -- ;
				if (intInputIssue == 0) {
					edtInputIssue.setText("1"); 
				}else {
					edtInputIssue.setText(intInputIssue+"");	
				}
			}
			break;
		case R.id.btn_intell_add_issue:
            intInputIssue = Integer.parseInt(edtInputIssue.getText().toString().trim())	 ;
            
          //快3
			if (lorID == Constants.LOT_ID_KUAI3) {
			       
				if (intInputIssue > Constants.ISSUE_K_3_MAX) {
					edtInputIssue.setText(Integer.toString(Constants.ISSUE_K_3_MAX));
				}else {
					intInputIssue ++ ;
					if (intInputIssue == Constants.ISSUE_K_3_MAX) {
						edtInputIssue.setText(Integer.toString(Constants.ISSUE_K_3_MAX));
					}else {
						edtInputIssue.setText(intInputIssue+"");
					}
				}
			 
			}
			//11选5
			else if (lorID == Constants.LOT_ID_11X5) {
				
				if (intInputIssue > Constants.ISSUE_11_5_MAX) {
					edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_5_MAX));
				}else {
					intInputIssue ++ ;
					if (intInputIssue == Constants.ISSUE_11_5_MAX) {
						edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_5_MAX));
					}else {
						edtInputIssue.setText(intInputIssue+"");
					}
				}
				
			}
			// 11运 夺金
			else if (lorID == Constants.LOT_ID_11YDJ) {
				
				if (intInputIssue > Constants.ISSUE_11_DJ_MAX) {
					edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_DJ_MAX));
				}else {
					intInputIssue ++ ;
					if (intInputIssue == Constants.ISSUE_11_DJ_MAX) {
						edtInputIssue.setText(Integer.toString(Constants.ISSUE_11_DJ_MAX));
					}else {
						edtInputIssue.setText(intInputIssue+"");
					}
				}
			 
			}
			// 重庆时时彩
			else if (lorID == Constants.LOT_ID_CQSSC) {
				
				if (intInputIssue > Constants.ISSUE_CQSS_MAX) {
					edtInputIssue.setText(Integer.toString(Constants.ISSUE_CQSS_MAX));
				}else {
					intInputIssue ++ ;
					if (intInputIssue == Constants.ISSUE_CQSS_MAX) {
						edtInputIssue.setText(Integer.toString(Constants.ISSUE_CQSS_MAX));
					}else {
						edtInputIssue.setText(intInputIssue+"");
					}
				}
				 
			}
            
			
			break;
		case R.id.btn_intell_sub_mul:
			intInputMul = Integer.parseInt(edtInputMul.getText().toString().trim())	 ;
			if (intInputMul < 1) {
				edtInputMul.setText("1");
			}else {
				intInputMul -- ;
				if (intInputMul == 0) {
					edtInputMul.setText("1");
				}else {
					edtInputMul.setText(intInputMul+"");	
				}
			}
			break;
		case R.id.btn_intell_add_mul:
			
			intInputMul = Integer.parseInt(edtInputMul.getText().toString().trim())	 ;
			if (intInputMul > issueMaxMul) {
				edtInputMul.setText(issueMaxMul+"");
			}else {
				intInputMul ++ ;
				if (intInputMul == issueMaxMul) {
					edtInputMul.setText(issueMaxMul+"");
				}else {
					edtInputMul.setText(intInputMul+"");	
				}
			}
			break;

		default:
			break;
		}

	}




}
